package com.coderlin.dao;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/03/11 15:56 <br>
 * @see com.coderlin.dao <br>
 */
public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            driver=properties.getProperty("Driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            //加载驱动，实现数据库操作，实例化Driver类对应的Class对象
            Class.forName(driver);
            //获取数据库连接
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  connection;
    }

    /**sql查询语句的公共方法
     * @author Coderlin
     * */
    public static ResultSet excute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params) throws SQLException {

        //connection= BaseDao.getConnection();
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    /**sql增删改的公共方法
    * @author Coderlin
    * */
    public static int excute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws SQLException {

        //connection= BaseDao.getConnection();
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }

    /**释放资源方法
     *
     * */
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        //关闭方法是否成功
        boolean flag = true;

        if(resultSet!=null){
            try {
                resultSet.close();
                //GC会进行回收
                resultSet=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if(preparedStatement!=null){
            try {
                preparedStatement.close();
                //GC会进行回收
                preparedStatement=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if(connection!=null){
            try {
                connection.close();
                //GC会进行回收
                connection=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }

}
